import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
import ddt
from selenium.webdriver.support.select import Select

from python.unit.param.util.JDBCUtils import get_sql_content

dp1 = get_sql_content('ecshop_user_register', 'user_name', 'user_email', 'pass_word',
                      'con_pass_word', 'user_msn', 'user_qq', 'work_tel', 'home_tel', 'tel', 'user_question',
                      'user_answer')


@ddt.ddt()
class TestRegisterEcShop(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(cls) -> None:
        # 快速 导包的快捷键  alt + enter
        service = Service(executable_path="../driver/geckodriver.exe")
        cls.driver = webdriver.Firefox(service=service)

        cls.driver.implicitly_wait(30)

    @ddt.data(*dp1)
    def test_registerEcshop(self, data):
        self.driver.get("http://192.168.1.118/ecshop/")
        self.driver.maximize_window()

        if self.driver.page_source.__contains__('退出'):
            self.driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/ul/li[1]/font/font/a[2]").click()
            self.register_ecshop_user(data)
        else:
            self.register_ecshop_user(data)

        page_source = self.driver.page_source
        # 通过 断言的 方式来 判断注册是否成功
        self.assertTrue(page_source.__contains__('退出'))

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.close()  # 只会 关闭一个 标签页
        # cls.driver.quit()  # 推出浏览器  全部关闭所有的标签页

        pass

    def register_ecshop_user(self, data):
        self.driver.find_element(By.CSS_SELECTOR, "#ECS_MEMBERZONE > a:nth-child(3) > img").click()
        self.driver.find_element(By.ID, "username").send_keys(data[0])
        self.driver.find_element(By.ID, "email").send_keys(data[1])
        self.driver.find_element(By.ID, "password1").send_keys(data[2])
        self.driver.find_element(By.ID, "conform_password").send_keys(data[3])
        self.driver.find_element(By.NAME, "extend_field1").send_keys(data[4])
        self.driver.find_element(By.NAME, "extend_field2").send_keys(data[5])
        self.driver.find_element(By.NAME, "extend_field3").send_keys(data[6])
        self.driver.find_element(By.NAME, "extend_field4").send_keys(data[7])
        self.driver.find_element(By.NAME, "extend_field5").send_keys(data[8])
        select = Select(self.driver.find_element(By.NAME, "sel_question"))
        select.select_by_index(data[9])
        self.driver.find_element(By.NAME, "passwd_answer").send_keys(data[10])

        # 获得 第一个测试 用例 中的 出错的情况 是否为  邮箱已占用
        text = self.driver.find_element(By.ID, "email_notice").text
        if not text.__eq__('* 可以注册'):
            print(text)

        self.driver.find_element(By.NAME, "Submit").click()


if __name__ == '__main__':
    pass
